package com.example.auv_660.autocontrolalgorithm.assistantsensor;

import android.annotation.SuppressLint;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;

import com.example.auv_660.MainActivity;

public class LocationSensor {
    private MainActivity mainActivity = MainActivity.getInstance();
    private LocationManager locationManager;
    private double values[] = new double[3];
    private static int startGPS = 0;

    private LocationListener locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(final Location location) {
            if (location != null)
            {
                values[0] = location.getLongitude();            // GPS经度
                values[1] = location.getLatitude();             // GPS纬度
                values[2] = location.getAccuracy();             // GPS精度
            }
            System.out.println("*************************locationChange");
            mainActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mainActivity.locationUpdates(location);
                }
            });
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {

        }
    };

    public LocationSensor()
    {
        if (mainActivity != null)
        {
            locationManager = (LocationManager) mainActivity.getSystemService(Context.LOCATION_SERVICE);
            StartLocation();
        }
    }

    @SuppressLint("MissingPermission")
    private void StartLocation()
    {
        final Location location;

        System.out.println("startGPS: " + startGPS);

        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, locationListener);

        location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

        if (location != null)
        {
            values[0] = location.getLongitude();
            values[1] = location.getLatitude();
            values[2] = location.getAccuracy();

            mainActivity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mainActivity.locationUpdates(location);
                }
            });
        }

        locationManager.removeUpdates(locationListener);

        mainActivity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 20, locationListener);
            }
        });
    }

    public void stopLocation()
    {
        if (locationManager != null)
        {
            locationManager.removeUpdates(locationListener);
            locationManager = null;
        }
    }

    @SuppressLint("MissingPermission")
    public double[] getValue(String work)
    {
        final Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null)
        {
            values[0] = location.getLongitude();
            values[1] = location.getLatitude();
            values[2] = location.getAccuracy();

            if (!work.contains("GPS"))
            {
                mainActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        mainActivity.locationUpdates(location);
                    }
                });
            }
        }
        return values;
    }

    @SuppressLint("MissingPermission")
    public Location getLocation()
    {
        final Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        return location;
    }
}
